package org.jflame.fx.control;

import java.util.Optional;

import org.jflame.fx.assets.JFLResources;

import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

/**
 * 自定义样式的Alert提示框.
 * <p>
 * 去掉边框，增加外层阴影效果作为边框，将headerPanel区作边框标题栏，增加关闭按钮和窗体拖动事件. <br>
 * 变为白色背景. 修改内部按钮样式
 * 
 * @author charles.zhang
 */
public class JFLAlert {

    private double xOffset;
    private double yOffset;
    private Alert alert;
    private AlertType alertType;

    public JFLAlert(String content, Window ownerWindow) {
        this(AlertType.NONE, content, ownerWindow);
    }

    public JFLAlert(AlertType alertType, String content, Window ownerWindow) {
        this.alertType = alertType;
        alert = new Alert(AlertType.NONE, content);
        if (ownerWindow != null) {
            alert.initOwner(ownerWindow);
            alert.initModality(Modality.WINDOW_MODAL);
        }

        DialogPane dialogPane = alert.getDialogPane();
        dialogPane.getStylesheets()
                .add(JFLResources.load("css/dialog.css")
                        .toExternalForm());
        dialogPane.getScene()
                .setFill(Color.TRANSPARENT);
        alert.initStyle(StageStyle.TRANSPARENT);

        initTitleArea();
        // initButtonArea();
    }

    public void show() {
        alert.show();
    }

    public Optional<ButtonType> showAndWait() {
        return alert.showAndWait();
    }

    public void close() {
        alert.close();
    }

    /* private void initButtonArea() {
        switch (alertType) {
            case CONFIRMATION:
                alert.getButtonTypes()
                        .addAll(ButtonType.OK, ButtonType.CANCEL);
                break;
            case WARNING:
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
            case ERROR:
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
            default:
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
        }
    }
    */
    private void initTitleArea() {
        switch (alertType) {
            case CONFIRMATION:
                alert.setHeaderText(JFLResources.getString("pub.confirm"));
                alert.getButtonTypes()
                        .addAll(ButtonType.OK, ButtonType.CANCEL);
                break;
            case WARNING:
                alert.setHeaderText(JFLResources.getString("pub.warn"));
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
            case ERROR:
                alert.setHeaderText(JFLResources.getString("pub.error"));
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
            default:
                alert.setHeaderText(JFLResources.getString("pub.tip"));
                alert.getButtonTypes()
                        .addAll(ButtonType.OK);
                break;
        }

        SVGGlyph closeIcon = new SVGGlyph(0, "CLOSE",
                "M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z",
                Color.DARKSLATEGRAY);
        closeIcon.setSize(9, 9);
        Button close = new Button();
        close.getStyleClass()
                .add("dialog-close-button");
        close.setGraphic(closeIcon);
        close.setCursor(Cursor.HAND);
        close.setOnMouseClicked((event) -> {
            alert.close();
        });

        alert.setGraphic(close);
        // 原header区作为边框标题栏，增加拖动事件和关闭按钮
        Node header = alert.getDialogPane()
                .lookup(".header-panel");
        header.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // 鼠标按下时，记下相对于 root左上角(0,0) 的 x, y坐标
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        header.setOnMouseDragged(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // 根据偏移量设置primaryStage新的位置
                Stage stage = (Stage) alert.getDialogPane()
                        .getScene()
                        .getWindow();
                stage.setX(event.getScreenX() - xOffset);
                stage.setY(event.getScreenY() - yOffset);
            }
        });

    }

    public static void info(String content) {
        info(content, null);
    }

    public static void info(String content, Window ownerWindow) {
        new JFLAlert(content, ownerWindow).show();
    }

    public static Optional<ButtonType> confirm(String content) {
        return confirm(content);
    }

    public static Optional<ButtonType> confirm(String content, Window ownerWindow) {
        return new JFLAlert(AlertType.CONFIRMATION, content, ownerWindow).showAndWait();
    }

    public static Optional<ButtonType> error(String content) {
        return error(content, null);
    }

    public static Optional<ButtonType> error(String content, Window ownerWindow) {
        return new JFLAlert(AlertType.ERROR, content, ownerWindow).showAndWait();
    }

    public static Optional<ButtonType> warn(String content, Window ownerWindow) {
        return new JFLAlert(AlertType.WARNING, content, ownerWindow).showAndWait();
    }

    public static Optional<ButtonType> warn(String content) {
        return warn(content, null);
    }

}
